﻿using System;
using System.Collections.Generic;
using System.Data;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Display;
using EAGL.Display;
using EAGL.Server;
using EAGL.Data;



namespace EAGL.Search
{
    public class AttributeSelector : IServerApplicable
    {

        #region ============================= PRIVATE ===========================

        protected IFeatureSelection featureSelection;
        protected string whereClause;
  
        protected bool serverMode;
        protected IServerContext serverContext;

        protected ICursor getSelectionAsCusror(string whereClause)
        {
            
            ISelectionSet ss = this.featureSelection.SelectionSet;
            ICursor cur = null;
            IQueryFilter qf = null;

            if (whereClause != "")
            {
                if (serverMode)
                {
                    qf = serverContext.CreateObject("esriGeoDatabase.QueryFilter") as IQueryFilter;
                }
                else
                {
                    qf = new QueryFilterClass();

                }

            }

            ss.Search(qf, false, out cur);
            return cur;
        }

        protected DataTable getSelectionAsDataTable(string whereClause)
        {
            ICursor cur = this.getSelectionAsCusror(whereClause);
            IFeatureCursor fcur = cur as IFeatureCursor;

            // Create The Table
            DataTableBuilder builder = new DataTableBuilder(cur.Fields);
            DataTable table = builder.DataTable;

            // Fill The Table
            DataTableAdapter adapter = new DataTableAdapter(cur);
            adapter.Fill(table);
            return table;
        }

        protected IFeatureClass getSelectionAsFeatureClass(string whereClause)
        {
            ICursor cur = this.getSelectionAsCusror(whereClause);
            IFeatureCursor fcur = cur as IFeatureCursor;

            // Create FeatureClass
            FeatureClassBuilder builder = new FeatureClassBuilder(cur.Fields);
            IFeatureClass fc = builder.FeatureClass;

            FeatureClassAdapter adapter = new FeatureClassAdapter(cur as IFeatureCursor);
            adapter.Fill(fc);
            return fc;
        }


        #endregion



        #region ============================= PUBLIC ===========================


        public AttributeSelector(IFeatureSelection featSel)
        {
            this.featureSelection = featSel;
        }

        public IFeatureSelection FeatureSelection
        {
            get { return this.featureSelection; }
            set { this.featureSelection = value; }
        }

        public string WhereClause
        {
            get { return whereClause; }
            set { this.whereClause = value; }
        }

        public bool ServerMode
        {
            get { return serverMode; }
            set { this.serverMode = value; }
        }

        public IServerContext ServerContext
        {
            get { return serverContext; }
            set { this.serverContext = value; }
        }

        
        public int SelectByAttribute(string whereClause, esriSelectionResultEnum selectionMethod, System.Drawing.Color selectionColor)
        {
            if (this.featureSelection == null)
            {
                throw (new Exception("Feature Selection not assigned."));
            }

            IQueryFilter qf;
            if (serverMode)
            {
                qf = serverContext.CreateObject("esriGeoDatabase.QueryFilter") as IQueryFilter;
            }
            else
            {
                qf = new QueryFilterClass();
            }

            qf.WhereClause = whereClause;
            featureSelection.SetSelectionSymbol = false;
            featureSelection.SelectionColor = ColorManager.GetESRIColor(selectionColor);
            featureSelection.SelectFeatures(qf, selectionMethod, false);
            return featureSelection.SelectionSet.Count;
        }

        public ICursor GetSelectionAsCusror(string whereClause)
        {
            if (this.featureSelection == null)
            {
                throw (new Exception("Feature Selection not assigned."));
            }

            return this.getSelectionAsCusror(whereClause);
        }

        public DataTable GetSelectionAsDataTable(string whereClause)
        {
            if (this.featureSelection == null)
            {
                throw (new Exception("Feature Selection not assigned."));
            }

            return this.getSelectionAsDataTable(whereClause);
        }

        public IFeatureClass GetSelectionAsFeatureClass(string whereClause)
        {
            if (this.featureSelection == null)
            {
                throw (new Exception("Feature Selection not assigned."));
            }

            return this.getSelectionAsFeatureClass(whereClause);
        }

        

        

        #endregion
    }
}
